package leetcode.p959;

/**
 * @author: trtan
 * @create: 2021-02-22 22:12
 * @description: 由斜杆划分区域，求区域个数
 **/
public class RegionsCutBySlashes {
    public static void main(String[] args) {
        String[] grid = new String[] {"//","/ "};
        System.out.println(new RegionsCutBySlashes().regionsBySlashes(grid));
    }
    /**
     * @author trtan
     * @date 2021/2/22 23:17
     * description: 将grid每个格子一拆为四
     * @param grid
     * @return boolean[][]
     */
    private boolean[][] reBuildGrid(String[] grid) {
        int N = grid.length;
        boolean[][] rebuildGrid = new boolean[4 * N][4 * N];
        for (int i = 0; i < N; i++) {
            String line = grid[i];
            int j = 0;
            while (j < line.length()) {
                if ('/' == line.charAt(j)) {
                    for (int k = 0; k < 4; k++) {
                        for (int l = 0; l < 4; l++) {
                            if (k + l == 3)rebuildGrid[4 * i + k][4 * j + l] = true;
                            else rebuildGrid[4 * i + k][4 * j + l] = false;
                        }
                    }
                } else if (' ' == line.charAt(j)){
                    for (int k = 0; k < 4; k++) {
                        for (int l = 0; l < 4; l++) {
                            rebuildGrid[4 * i + k][4 * j + l] = false;
                        }
                    }
                } else {
                    for (int k = 0; k < 4; k++) {
                        for (int l = 0; l < 4; l++) {
                            if (k == l)rebuildGrid[4 * i + k][4 * j + l] = true;
                            else rebuildGrid[4 * i + k][4 * j + l] = false;
                        }
                    }
                }
                j++;
            }
        }
        return rebuildGrid;
    }
    /**
     * @author trtan
     * @date 2021/2/22 23:17
     * description:  dfs遍历连通块
     * @param x
     * @param y
     * @param grid
     * @param n
     */
    private void dfs(int x, int y, boolean[][] grid, int n) {
        grid[x][y] = true;
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                if (i * j == 0) {
                    int dx = x + i;
                    int dy = y + j;
                    if (dx < 0 || dx >= n || dy < 0 || dy >= n) continue;
                    if (grid[dx][dy]) continue;
                    dfs(dx, dy, grid, n);
                }
            }
        }
    }
    public int regionsBySlashes(String[] grid) {
        int result = 0;
        boolean[][] rebuildGrid = reBuildGrid(grid);
        for (int i = 0; i < rebuildGrid.length; i++) {
            for (int j = 0; j < rebuildGrid.length; j++) {
                if (!rebuildGrid[i][j]) {
                    result ++;
                    dfs(i, j, rebuildGrid, rebuildGrid.length);
                    for (int k = 0; k < rebuildGrid.length; k++) {
                        for (int l = 0; l < rebuildGrid.length; l++) {
                            System.out.print(rebuildGrid[k][l]);
                        }
                        System.out.println();
                    }
                    System.out.println();
                }
            }
            System.out.println();
        }
        return result;
    }
}
